package com.ahui.ydd.controller;

import com.ahui.ydd.common.R;

import com.ahui.ydd.dto.SysUserDto;
import com.ahui.ydd.dto.PageDto;
import com.ahui.ydd.entity.SysUser;
import com.ahui.ydd.service.SysUserSerive;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;


@Slf4j
@RestController
@RequestMapping("/sys_user")
public class SysUserController {
    @Autowired
    private SysUserSerive sysUserSerive;

    @GetMapping("/list")  //管理员列表分页page
    public R<Page> page(String loginName, String status, String phone, Integer page , Integer limit ){
        log.info("loginName={},status={},phone={},page={},limit={}",loginName,status,phone,page,limit);

        //构造分页构造器
        Page pageInfo = new Page(page,limit);
        //构造条件构造器
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(loginName),SysUser::getLoginName,loginName);
        queryWrapper.eq(StringUtils.isNotEmpty(status),SysUser::getStatus,status);
        queryWrapper.like(StringUtils.isNotEmpty(phone),SysUser::getPhone,phone);
        queryWrapper.eq(SysUser::getDelFlag, "0");
        //添加排序条件
        queryWrapper.orderByDesc(SysUser::getUpdateDate);
        //执行查询
        sysUserSerive.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }

    @GetMapping("/del")   //管理员删除
    public R<String> delUser(String id){
        log.info("删除管理员，id为:{}",id);
        //将删除字段 set del_flag = 1
        LambdaUpdateWrapper<SysUser> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(SysUser::getId,id).set(SysUser::getDelFlag,1);
        sysUserSerive.update(wrapper);


        return R.success("");
    }

    @PostMapping("/add")     //管理员添加
    public R<String> add(HttpServletRequest request, @RequestBody SysUserDto sysUserDto){
        log.info("新增管理员，管理员信息:{}",sysUserDto.toString());
        //设置密码，需要进行md5加密处理
        sysUserDto.setPassword(DigestUtils.md5DigestAsHex(sysUserDto.getPassword().getBytes()));
        sysUserDto.setId(UUID.randomUUID().toString());
        sysUserDto.setRoleId(JSON.toJSONString(sysUserDto.getRoleId()));
        sysUserDto.setDefaultData("1");

        sysUserSerive.save(sysUserDto);

        return R.success("");

    }

    @GetMapping("/rowInfo")     //管理员详情getById
    public R<SysUserDto> rowInfo(String id){
        log.info("根据id查询管理员详情");
        SysUserDto sysUserDto = new SysUserDto();
        SysUser sysUser = sysUserSerive.getById(id);
        sysUser.setPassword(null);

        String roleId =sysUser.getRoleId();
        List<String> roleIds= JSON.parseObject(roleId,List.class);

        BeanUtils.copyProperties(sysUser,sysUserDto);
        sysUserDto.setRoleIds(roleIds);
        return R.success(sysUserDto);

    }

    @PostMapping("/update")  //管理员修改
    public R<String> update(HttpServletRequest request,@RequestBody SysUserDto sysUserDto){

        log.info("更新管理员：{}",sysUserDto.toString());
        String password =sysUserDto.getPassword();
        if (password != null) {
            password = DigestUtils.md5DigestAsHex(password.getBytes());
            sysUserDto.setPassword(password);
        }
        sysUserDto.setRoleId(JSON.toJSONString(sysUserDto.getRoleIds()));
        sysUserSerive.updateById(sysUserDto);

        return R.success(" ");
    }

}
